package net.pinnoocle.playservice.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.enums.AuditStatusEnums;
import net.pinnoocle.playcommon.exception.ShopException;
import net.pinnoocle.playcommon.response.ErrorCode;
import net.pinnoocle.playcommon.utils.UploadWxMedia;
import net.pinnoocle.playcommon.utils.WechatUtils;
import net.pinnoocle.playdao.entity.*;
import net.pinnoocle.playdao.entity.wechat.*;
import net.pinnoocle.playservice.service.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ye
 * @Description
 * @create 2021-11-19 10:08
 */
@Service("wechatServiceImpl")
@Slf4j
public class WechatServiceImpl {

    @Value("${upload.folder}")
    private String uploadFolder;

    @Autowired
    WxServiceImpl wxService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    RegisterWechatServiceImpl registerWechatService;
    @Autowired
    WechatSubmitServiceImpl wechatSubmitService;
    @Autowired
    ShopWechatSettingServiceImpl shopWechatSettingService;
    @Autowired
    WechatCommitServiceImpl wechatCommitService;
    @Autowired
    WechatSetNameServiceImpl wechatSetNameService;
    @Autowired
    CommonService commonService;
    @Autowired
    ShopServiceImpl shopService;

    /**
     * 获取小程序草稿列表
     * @return
     */
    public List<TemplatedRaftListVo> templatedRaftList(){
        String componentAccessToken = wxService.getComponentAccessToken();
        JSONObject templatedRaftList = WechatUtils.getTemplatedRaftList(componentAccessToken);
        if(Objects.nonNull(templatedRaftList)){
            JSONArray draftList = templatedRaftList.getJSONArray("draft_list");
            List<TemplatedRaftListVo> templatedRaftListVos = draftList.toJavaList(TemplatedRaftListVo.class);
            for (TemplatedRaftListVo item : templatedRaftListVos){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formatDate = sdf.format(new Date(Long.parseLong(item.getCreateTime())*1000));
                item.setCreateDate(formatDate);
            }
            return templatedRaftListVos;
        }
        return new ArrayList<>();
    }

    /**
     * 草稿箱添加到代码库
     * @param draftId
     * @return
     */
    public boolean addTemplate(Integer draftId){
        String componentAccessToken = wxService.getComponentAccessToken();
        String result = WechatUtils.addToTemplate(draftId, componentAccessToken);
        if(Objects.equals(result, "success")){
            return true;
        }else{
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
        }
    }

    /**
     * 获取模板库列表
     * @return
     */
    public List<TemplateListVo> templateList(){
        String componentAccessToken = wxService.getComponentAccessToken();
        JSONObject templateList = WechatUtils.getTemplateList(componentAccessToken);
        if(Objects.nonNull(templateList)){
            JSONArray templateListArray = templateList.getJSONArray("template_list");
            List<TemplateListVo> templateListVos = templateListArray.toJavaList(TemplateListVo.class);
            for (TemplateListVo item : templateListVos){
                //格式化时间
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formatDate = sdf.format(new Date(Long.parseLong(item.getCreateTime())*1000));
                item.setCreateDate(formatDate);
                //格式化状态
                String desc = AuditStatusEnums.getDesc(item.getAuditStatus());
                item.setAuditStatusName(desc);
            }
            return templateListVos;
        }
        return new ArrayList<>();
    }

    /**
     * 上传代码
     * @return
     */
    public boolean commitCode(CommitCodeParams params){
        String shopId = userService.getGlobalShopId();
        ShopWechatSetting wechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                .lambda()
                .eq(ShopWechatSetting::getShopId, shopId));
        if(Objects.isNull(wechatSetting)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"小程序信息未配置");
        }
        String appid = wechatSetting.getAppId();
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        if(StringUtils.isBlank(authorizerToken)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"调用令牌获取失败，请联系管理员");
        }
        Integer ShopWechatId = wechatSetting.getId();
        HashMap<String, String> map = new HashMap<>();
        String templateId = params.getTemplateId();
        String userVersion = params.getUserVersion();
        String userDesc = params.getUserDesc();
        map.put("template_id",templateId);
        map.put("user_version",userVersion);
        map.put("user_desc",userDesc);
        String exJson = "{\"extAppid\":\""+appid+"\",\"ext\":{\"ShopWechatId\":\""+ShopWechatId+"\"},\"window\":{}}";
        map.put("ext_json",exJson);
        String result = WechatUtils.commitCode(authorizerToken, map);
        if(Objects.equals(result,"success")){
            WechatCommit commit = wechatCommitService.getOne(new QueryWrapper<WechatCommit>()
                    .lambda()
                    .eq(WechatCommit::getShopId, shopId),false);
            if(Objects.isNull(commit)){
                WechatCommit wechatCommit = new WechatCommit();
                wechatCommit.setShopId(shopId);
                wechatCommit.setUserVersion(params.getUserVersion());
                wechatCommit.setUserDesc(params.getUserDesc());
                wechatCommit.setCreatedTime(new Date());
                wechatCommitService.save(wechatCommit);
            }else{
                commit.setUserVersion(params.getUserVersion());
                commit.setUserDesc(params.getUserDesc());
                commit.setCreatedTime(new Date());
                wechatCommitService.updateById(commit);
            }
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 提交代码记录
     * @return
     */
    public WechatCommit commitRecord(){
        String shopId = userService.getGlobalShopId();
        return wechatCommitService.getOne(new QueryWrapper<WechatCommit>()
                .lambda()
                .eq(WechatCommit::getShopId, shopId),false);
    }

    /**
     * 获取已上传的代码的页面列表
     * @return
     */
    public List<String> getPages(){
        String authorizerToken = wxService.getAuthorizerToken("111");
        JSONObject wxPage = WechatUtils.getWxPage(authorizerToken);
        if(Objects.nonNull(wxPage)){
            JSONArray pages = wxPage.getJSONArray("page_list");
            return pages.toJavaList(String.class);
        }
        return new ArrayList<>();
    }

    /**
     * 获取审核时可填写的类目信息
     * @return
     */
    public List<WechatCategoryList> wxCategory(){
        String authorizerToken = wxService.getAuthorizerToken("3333");
        JSONObject category = WechatUtils.getCategory(authorizerToken);
        if(Objects.nonNull(category)){
            JSONArray categoryList = category.getJSONArray("category_list");
            return categoryList.toJavaList(WechatCategoryList.class);
        }
        return new ArrayList<>();
    }

    /**
     * 添加类目
     * @return
     */
    public boolean addCategory(){
        String shopId = userService.getGlobalShopId();
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String result = WechatUtils.addCategory(authorizerToken);
        if(Objects.equals(result, "success")){
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 刷新令牌
     * @return
     */
    public String refreshToken(){
        String shopId = userService.getGlobalShopId();
        return wxService.getAuthorizerToken(shopId);
    }

    /**
     *  提交审核
     * @param params
     * @return
     */
    public boolean submitAudit(SubmitAuditParams params){
        String shopId = userService.getGlobalShopId();
        ShopWechatSetting wechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                .lambda()
                .eq(ShopWechatSetting::getShopId, shopId));
        if(Objects.isNull(wechatSetting)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"小程序信息未配置");
        }
        String appid = wechatSetting.getAppId();
        if(StringUtils.isBlank(appid)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"小程序appId未配置");
        }
        if(wechatSetting.getSetHeadImage() == 0){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"请先配置小程序头像");
        }
        if(wechatSetting.getSetNickName() != 1){
            String message = "";
            switch (wechatSetting.getSetNickName()){
                case 0:
                    message = "请先配置小程序名称";
                    break;
                case 2:
                    message = "小程序名称正在审核，暂时不能提交小程序";
                    break;
                case 3:
                    message = "小程序名称审核失败，请重新设置小程序名称";
                    break;
                default:
                    break;
            }
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,message);
        }
        if(wechatSetting.getSetSignature() == 0){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"请先配置小程序功能简介");
        }
        if(wechatSetting.getPrivacySetting() == 0){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"请先配置小程序用户隐私保护指引");
        }
        if(wechatSetting.getAddCategory() == 0){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"请先配置小程序类目");
        }
        //是否存在待审核的
        WechatSubmit submit = wechatSubmitService.getOne(new QueryWrapper<WechatSubmit>()
                .lambda()
                .eq(WechatSubmit::getShopId,shopId)
                .eq(WechatSubmit::getResult,0),false);
        if(Objects.nonNull(submit)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"代码已经送审，耐心等待");
        }
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String postJson = JSONObject.toJSONString(params);
        String result = WechatUtils.submitAudit(postJson, authorizerToken);
        if(StringUtils.isNotBlank(result)){
            WechatSubmit wechatSubmit = new WechatSubmit();
            wechatSubmit.setShopId(shopId);
            wechatSubmit.setVersionDesc(params.getVersion_desc());
            wechatSubmit.setFeedbackInfo(params.getFeedback_info());
            wechatSubmit.setAuditId(result);
            wechatSubmit.setAppid(appid);
            wechatSubmit.setCreatedTime(new Date());
            wechatSubmitService.save(wechatSubmit);
            return true;
        }
        return false;
    }

    /**
     * 最新提交审核记录
     * @return
     */
    public WechatSubmit submitRecord(){
        String shopId = userService.getGlobalShopId();
        return wechatSubmitService.getOne(new QueryWrapper<WechatSubmit>()
                .lambda()
                .eq(WechatSubmit::getShopId, shopId)
                .orderByDesc(WechatSubmit::getCreatedTime),false);
    }

    /**
     * 发布已通过审核的小程序
     * @return
     */
    public boolean releaseWechat(){
        String shopId = userService.getGlobalShopId();
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String result = WechatUtils.releaseWx(authorizerToken);
        if(Objects.equals(result, "success")){
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 修改小程序头像
     * @param mediaId
     * @return
     */
    public boolean setHeadImage(String mediaId){
        if(StringUtils.isBlank(mediaId)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"请上传小程序头像");
        }
        String shopId = userService.getGlobalShopId();
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String result = WechatUtils.setHeadImage(authorizerToken, mediaId);
        if(Objects.equals(result,"success")){
            ShopWechatSetting shopWechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                    .lambda()
                    .eq(ShopWechatSetting::getShopId, shopId));
            shopWechatSetting.setSetHeadImage(1);
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 设置小程序昵称
     * @param params
     * @return
     */
    public boolean setNickName(SaveWechatNickNameParams params){
        String shopId = userService.getGlobalShopId();
        WechatSetName wechatSetNameOne = wechatSetNameService.getOne(new QueryWrapper<WechatSetName>()
                .lambda()
                .eq(WechatSetName::getShopId, shopId)
                .eq(WechatSetName::getResult, 0),false);
        if(Objects.nonNull(wechatSetNameOne)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"小程序名称正在审核，请勿重复提交");
        }
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        ResultCommonVo resultCommonVo = this.checkNickName(authorizerToken, params.getNickName());
        //名称非法
        if(resultCommonVo.getErrCode() != 0 && resultCommonVo.getErrCode() != 99){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,resultCommonVo.getErrMsg());
        }
        //命中了关键词
        if(resultCommonVo.getErrCode() == 99 && StringUtils.isBlank(params.getLicenseMediaId())){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,resultCommonVo.getErrMsg());
        }
        String result = WechatUtils.setNickName(authorizerToken, params.getNickName(),params.getLicenseMediaId());
        if(Objects.equals(result,"success") || Objects.equals(result,"check")){
            // 1 审核通过 2 已提交待审核
            int resultStatus = Objects.equals(result,"success") ? 1 : 2;
            //记录提交日志
            WechatSetName wechatSetName = new WechatSetName();
            wechatSetName.setShopId(shopId);
            wechatSetName.setNickName(params.getNickName());
            wechatSetName.setLicenseMediaId(params.getLicenseMediaId());
            wechatSetName.setCreatedTime(new Date());
            wechatSetName.setResult(resultStatus == 1 ? 1 : 0);
            wechatSetNameService.save(wechatSetName);
            ShopWechatSetting shopWechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                    .lambda()
                    .eq(ShopWechatSetting::getShopId, shopId));
            shopWechatSetting.setSetNickName(resultStatus);
            shopWechatSettingService.updateById(shopWechatSetting);
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 检测小程序名称是否命中关键词
     * @param authorizerToken
     * @param nickName
     * @return
     */
    public ResultCommonVo checkNickName(String authorizerToken,String nickName){
        ResultCommonVo resultCommonVo = new ResultCommonVo();
        JSONObject jsonObject = WechatUtils.checkNickName(authorizerToken, nickName);
        Integer errcode = jsonObject.getInteger("errcode");
        if(errcode == 0){
            Boolean hitCondition = jsonObject.getBoolean("hit_condition");
            if(hitCondition){
                String errmsg = jsonObject.getString("wording");
                resultCommonVo.setErrCode(99);
                resultCommonVo.setErrMsg(errmsg);
            }else{
                resultCommonVo.setErrCode(0);
                resultCommonVo.setErrMsg("ok");
            }
        }else{
            String errmsg = jsonObject.getString("errmsg");
            resultCommonVo.setErrCode(errcode);
            resultCommonVo.setErrMsg(errmsg);
        }
        return resultCommonVo;
    }

    /**
     * 设置小程序简介
     * @param params
     * @return
     */
    public boolean setSignature(SaveWechatSignatureParams params){
        String shopId = userService.getGlobalShopId();
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String result = WechatUtils.setSignature(authorizerToken, params.getSignature());
        if(Objects.equals(result,"success")){
            ShopWechatSetting shopWechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                    .lambda()
                    .eq(ShopWechatSetting::getShopId, shopId));
            shopWechatSetting.setSetSignature(1);
            shopWechatSettingService.updateById(shopWechatSetting);
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 配置小程序用户隐私保护指引
     * @param params
     * @param shopId
     * @return
     */
    public boolean setPrivacySetting(SetPrivacySettingParams params,String shopId){
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String email = params.getContact_email();
        String phone = params.getContact_phone();
        if(StringUtils.isBlank(email) && StringUtils.isBlank(phone)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"邮箱和手机号至少填1种");
        }
        JSONObject jsonObject = new JSONObject();
        OwnerSettingParams ownerSettingParams = new OwnerSettingParams();
        if(StringUtils.isNotBlank(email)){
            ownerSettingParams.setContact_email(email);
        }else if(StringUtils.isNotBlank(phone)){
            ownerSettingParams.setContact_phone(phone);
        }
        ownerSettingParams.setNotice_method("公告");
        jsonObject.put("owner_setting",ownerSettingParams);
        ArrayList<SettingListPrivacy> privacyList = new ArrayList<>();

        SettingListPrivacy settingListPrivacy1 = new SettingListPrivacy();
        settingListPrivacy1.setPrivacy_key("UserInfo");
        settingListPrivacy1.setPrivacy_text("在系统显示用户的身份信息");
        privacyList.add(settingListPrivacy1);

        SettingListPrivacy settingListPrivacy2 = new SettingListPrivacy();
        settingListPrivacy2.setPrivacy_key("Location");
        settingListPrivacy2.setPrivacy_text("给用户匹配最合适的服务内容");
        privacyList.add(settingListPrivacy2);

        SettingListPrivacy settingListPrivacy3 = new SettingListPrivacy();
        settingListPrivacy3.setPrivacy_key("Album");
        settingListPrivacy3.setPrivacy_text("生成个人专属海报");
        privacyList.add(settingListPrivacy3);

        SettingListPrivacy settingListPrivacy4 = new SettingListPrivacy();
        settingListPrivacy4.setPrivacy_key("MessageFile");
        settingListPrivacy4.setPrivacy_text("获取用户提交的资料，进行客户身份补充");
        privacyList.add(settingListPrivacy4);

        SettingListPrivacy settingListPrivacy5 = new SettingListPrivacy();
        settingListPrivacy5.setPrivacy_key("AlbumWriteOnly");
        settingListPrivacy5.setPrivacy_text("让用户保存个人专属海报");
        privacyList.add(settingListPrivacy5);

        SettingListPrivacy settingListPrivacy6 = new SettingListPrivacy();
        settingListPrivacy6.setPrivacy_key("PhoneNumber");
        settingListPrivacy6.setPrivacy_text("及时联系客户以及服务回访");
        privacyList.add(settingListPrivacy6);

        jsonObject.put("setting_list",privacyList);
        jsonObject.put("privacy_ver",2);
        String result = WechatUtils.setPrivacySetting(authorizerToken, JSONObject.toJSONString(jsonObject));
        if(Objects.equals(result,"success")){
            //设置配置状态
            ShopWechatSetting wechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                    .lambda()
                    .eq(ShopWechatSetting::getShopId, shopId));
            if(Objects.nonNull(wechatSetting)){
                wechatSetting.setPrivacySetting(1);
                shopWechatSettingService.updateById(wechatSetting);
            }
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
    }

    /**
     * 一键设置默认值
     */
    public boolean fastSetDefaultValue(String registerId) throws Exception {
        RegisterWechat registerWechat = registerWechatService.getOne(new QueryWrapper<RegisterWechat>()
                .lambda()
                .eq(RegisterWechat::getRegisterId, registerId));
        if(Objects.nonNull(registerWechat)){
            String shopId = registerWechat.getShopId();
            ShopWechatSetting shopWechatSetting = shopWechatSettingService.getOne(new QueryWrapper<ShopWechatSetting>()
                    .lambda()
                    .eq(ShopWechatSetting::getShopId, shopId));
            String authorizerToken = wxService.getAuthorizerToken(shopId);
            //设置小程序昵称
            String nickName = registerWechat.getNickName();
            WechatSetName wechatSetNameOne = wechatSetNameService.getOne(new QueryWrapper<WechatSetName>()
                    .lambda()
                    .eq(WechatSetName::getShopId, shopId)
                    .eq(WechatSetName::getResult, 0),false);
            if(StringUtils.isNotBlank(nickName) && Objects.isNull(wechatSetNameOne)){
                String mediaId = "";
                //检测小程序名称是否命中关键词
                ResultCommonVo resultCommonVo = this.checkNickName(authorizerToken, nickName);
                if(resultCommonVo.getErrCode() == 0 || resultCommonVo.getErrCode() == 99){
                    //命中了关键词
                    if(resultCommonVo.getErrCode() == 99){
                        String businessLicense = registerWechat.getBusinessLicense();
                        int pos = businessLicense.indexOf("upload");
                        String fileName = businessLicense.substring(pos+7);
                        String realPath = uploadFolder + fileName;
                        //上传微信临时素材库获取mediaId
                        mediaId = UploadWxMedia.UploadMedia(realPath, authorizerToken);
                    }
                    String result = WechatUtils.setNickName(authorizerToken,nickName,mediaId);
                    if(Objects.equals(result,"success") || Objects.equals(result,"check")){
                        // 1 审核通过 2 已提交待审核
                        int resultStatus = Objects.equals(result,"success") ? 1 : 2;
                        shopWechatSetting.setSetNickName(resultStatus);
                    }
                }
            }
            //设置功能简介
            String signature = registerWechat.getSignature();
            if(StringUtils.isNotBlank(signature)){
                String result = WechatUtils.setSignature(authorizerToken,signature);
                if(Objects.equals(result,"success")){
                    shopWechatSetting.setSetSignature(1);
                }
            }
            //设置头像
            String headImage = registerWechat.getHeadImage();
            if(StringUtils.isNotBlank(headImage)){
                int pos = headImage.indexOf("upload");
                String fileName = headImage.substring(pos+7);
                String realPath = uploadFolder + fileName;
                //上传微信临时素材库获取mediaId
                String mediaId = UploadWxMedia.UploadMedia(realPath, authorizerToken);
                if(StringUtils.isNotBlank(mediaId)){
                    String result = WechatUtils.setHeadImage(authorizerToken, mediaId);
                    if(Objects.equals(result,"success")){
                        shopWechatSetting.setSetHeadImage(1);
                    }
                }
            }
            //配置小程序用户隐私保护指引
            String componentPhone = registerWechat.getComponentPhone();
            SetPrivacySettingParams setPrivacySettingParams = new SetPrivacySettingParams();
            setPrivacySettingParams.setContact_phone(componentPhone);
            boolean setPrivacySettingResult = setPrivacySetting(setPrivacySettingParams,shopId);
            if(setPrivacySettingResult){
                shopWechatSetting.setPrivacySetting(1);
            }
            //设置默认类目
            String addCategoryResult = WechatUtils.addCategory(authorizerToken);
            if(Objects.equals(addCategoryResult, "success")){
                shopWechatSetting.setAddCategory(1);
            }
            //添加默认服务器域名
            WechatUtils.addDefaultWxDomain(authorizerToken);
            //添加默认业务域名
            WechatUtils.addDefaultWebDomain(authorizerToken);
            shopWechatSettingService.updateById(shopWechatSetting);
            return true;
        }
        throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"门店没有注册小程序记录");
    }

    /**
     * 已存在的小程序设置默认值
     * @param shopId
     * @return
     */
    public boolean fastSetDefaultValueWithExists(String shopId,String phone){
        //配置小程序用户隐私保护指引
        SetPrivacySettingParams setPrivacySettingParams = new SetPrivacySettingParams();
        setPrivacySettingParams.setContact_phone(phone);
        boolean setPrivacySettingResult = setPrivacySetting(setPrivacySettingParams, shopId);
        log.info("setPrivacySettingResult==={}",setPrivacySettingResult);
        //设置默认类目
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        String addCategoryResult = WechatUtils.addCategory(authorizerToken);
        log.info("addCategoryResult==={}",addCategoryResult);
        //添加默认服务器域名
        WechatUtils.addDefaultWxDomain(authorizerToken);
        //添加默认业务域名
        WechatUtils.addDefaultWebDomain(authorizerToken);
        return true;
    }

    /**
     * 小程序基本信息
     * @return
     */
    public WeChatBasicInfoVo basicInfoVo(){
        String shopId = userService.getGlobalShopId();
        String authorizerToken = wxService.getAuthorizerToken(shopId);
        JSONObject basicInfo = WechatUtils.getBasicInfo(authorizerToken);
        if(Objects.nonNull(basicInfo)){
            WeChatBasicInfoVo weChatBasicInfoVo = basicInfo.toJavaObject(WeChatBasicInfoVo.class);
            WechatSetName setOne = wechatSetNameService.getOne(new QueryWrapper<WechatSetName>().lambda()
                    .eq(WechatSetName::getShopId, shopId)
                    .orderByDesc(WechatSetName::getCreatedTime), false);
            if(Objects.nonNull(setOne)){
                NickNameInfo nickNameInfo = weChatBasicInfoVo.getNickNameInfo();
                nickNameInfo.setResult(setOne.getResult());
                nickNameInfo.setReason(setOne.getReason());
                weChatBasicInfoVo.setNickNameInfo(nickNameInfo);
            }
            return weChatBasicInfoVo;
        }
        return null;
    }

    /**
     * 获取mediaId
     * @param file
     * @return
     */
    public String getMediaId(MultipartFile file) throws Exception {
        String fileName = commonService.uploadImageToWx(file);
        log.info("fileName=="+fileName);
        if(StringUtils.isNotBlank(fileName)){
            String shopId = userService.getGlobalShopId();
            String authorizerToken = wxService.getAuthorizerToken(shopId);
            return UploadWxMedia.UploadMedia(fileName, authorizerToken);
        }
        return "";
    }
}
